import setting from "@/api/modules/setting";
import {
  AUDIOS
} from "@/config/resources";
const state = {
  config: {},
  share: {},
  // 是否开启背景音乐
  IS_OPEN_BG_MUSIC: true,
  // 是否开启游戏音效
  IS_OPEN_GAME_AUDIO: true
};

const actions = {
  getConfig(context, current) {
    return setting.get().then(response => {
      context.commit("SET_CONFIG", response.data.data);
      return response;
    });
  },
  getShare(context, current) {
    return setting.share().then(response => {
      context.commit("SET_SHARE", response.data.data);
      return response;
    });
  },

};

const mutations = {
  SET_CONFIG(state, data) {
    state.config = data;
  },
  SET_SHARE(state, data) {
    state.share = data;
  },
  // 背景音乐开关
  TOGGLE_BG_MUSIC(state) {
    const bgAudioManager = uni.getBackgroundAudioManager();
    if (state.IS_OPEN_BG_MUSIC) {
      bgAudioManager.pause();
    } else {
      if (bgAudioManager.src === AUDIOS.AUDIO_BG_MUSIC) {
        bgAudioManager.play();
      } else {
        bgAudioManager.src = AUDIOS.AUDIO_BG_MUSIC;
      }
    }
    state.IS_OPEN_BG_MUSIC = !state.IS_OPEN_BG_MUSIC;
  },
  SET_GAME_AUDIO(state,bool = true) {
    state.IS_OPEN_GAME_AUDIO = bool;
  }
};


export default {
  namespaced: true,
  state,
  actions,
  mutations
};
